$package('jeecg.TostHrPersonyearinfo');

//人员
$("#xhrName").combobox({
	url:urls['msUrl']+"tostHrBaseinfo/hrNameList.do",
	cache: false,  //tostHrDepartinfo/getDepartBypost  tostHrBaseinfo
   // panelHeight: 'auto',//自动高度适合    去掉，显示滚动条
	dataType:'json',
	valueField:'id',
	textField:'hrName',
	multiple:false,
	formatter:function(result){
		 var s= "<span> <input type='hidden' class='selectId' style='vertical-align: middle' id='selectId_"+result.id+"' value='"+result.hrName+"'>"+result.hrName+"</span>";
		 return s;
	},
	onSelect:function(result){
		document.getElementById("hrId").value=result.id;
		document.getElementById("hrName").value=result.hrName;
		/*document.getElementById("typedispaly").style.display= 'inline';*/
		$("#selectId_"+result.id).attr("checked", true);
	},
	onUnselect:function(result){
		$("#selectId_"+result.id).attr("checked", false);
	},
	onChange:function(){
		/*document.getElementById("typedispaly").style.display= 'none';*/
		document.getElementById("hrId").value=null;
		document.getElementById("hrName").value=null;
    }
});
jeecg.TostHrPersonyearinfo = function(){
	var _box = null;
	var _this = {
		toList:function(parentId){
				_box.form.search.resetForm();
				if(parentId){
					$('#search_parentId').val(parentId);
					$('#btnback').linkbutton('enable');
					_box.grid.datagrid('hideColumn','childMenus');
				}else{
					$('#btnback').linkbutton('disable');
					$('#btnback').linkbutton('disable');
					_box.grid.datagrid('showColumn','childMenus');
				}
				_box.handler.refresh();
		},
		//设置默认按钮数据
		addDefBtns:function(){
			var defaultBtns= [
				{"btnName":"添加","menuid":2,"actionUrls":"save.do","btnType":"add"},
				{"btnName":"修改","menuid":2,"actionUrls":"getId.do|save.do","btnType":"edit"},
				{"btnName":"删除","menuid":2,"actionUrls":"delete.do","btnType":"remove"}
			];
			var tbline = $(".tb-line:visible");
			var btnType = $("input[name='btnType']",tbline);
			$.each(defaultBtns,function(i,btn){
				var isExists = false;
				//判断按钮类型是否存在
				if(btnType.length > 0){
					for(var i =0; i < btnType.length; i++){
						if(btnType.eq(i).val() == btn.btnType){
							isExists = true;
							break;
						}
					}
				}
				if(!isExists){
					_this.addLine(btn);
				}
			});
		},
		addLine: function(data){
			var table = $("#btn-tb");
			var	html = "<tr class='tb-line'>";
			html+=	   "	<td align='center'><span  class='newFlag red'>*</span></td>";
			html+=	   "	<td><input name=\"btnName\" class=\"easyui-validatebox text-name\" style=\"width:100%\" data-options=\"required:true\"></td>";
			html+=	   "	<td><input name=\"btnType\" class=\"easyui-validatebox text-name\" style=\"width:100%\" data-options=\"required:true\"></td>";
			html+=	   "	<td><input name=\"actionUrls\" class=\"easyui-validatebox text-desc\" style=\"width:100%\"  ></td>";
			html+=	   "	<td align='center'><a class=\"easyui-linkbutton remove-btn\"  iconCls=\"icon-remove\" plain=\"true\"></a>";
			html+=	   "	<input class=\"hidden\" name=\"btnId\">";
			html+=	   "	<input class=\"hidden\" name=\"deleteFlag\">";
			html+=	   "	</td>";
			html+=	   "</tr>";
			var line = $(html);
			//版定删除按钮事件
			$(".remove-btn",line).click(function(){
				jeecg.confirm('Confirm','Are you sure you want to delete record?',function(r){
					if(r){
						_this.delLine(line);
					}
				})
			});
			if(data){
				if(data.id){
					$(".newFlag",line).html(''); //清空新增标记
				}
				$("input[name='btnId']",line).val(data.id);
				$("input[name='btnName']",line).val(data.btnName);
				$("input[name='btnType']",line).val(data.btnType);
				$("input[name='actionUrls']",line).val(data.actionUrls);
			}
			$.parser.parse(line);//解析esayui标签
			table.append(line);
			
		},
		//删除全部
		delAllLine:function(b){
			if(b){
				$(".tb-line").remove();
			}else{
				$(".tb-line").each(function(i,line){
					_this.delLine($(line));
				});
			}
		},
		//删除单行
		delLine:function(line){
			if(line){
				var btnId = $("input[name='btnId']",line).val();
				if(btnId){
					$("input[name='deleteFlag']",line).val(1); //设置删除状态
					line.fadeOut();
				}else{
					line.fadeOut("fast",function(){
						 $(this).remove();
					});
				}
			}
		},
		config:{
  			action:{
  				save:'', //新增&修改 保存Action  
  				getId:'',//编辑获取的Action
  				delele:''//删除数据的Action
  			},
  			event:{
  				add : function(){
  					_this.delAllLine(true);//清空已有的数据
  					_box.handler.add();//调用add方法
					var parentId =$('#search_parentId').val();
					if(parentId){
						$("#edit_parentId").val(parentId);
					}
				},
				edit:function(){
					_this.delAllLine(true);
					_box.handler.edit(function(result){
					  if(result!=null && typeof(result.data.btns)!= "undefined"){	
						$.each(result.data.btns,function(i,btn){
							_this.addLine(btn);
						});
					  }
					});
				},
				search: function(callback){
					var type = $('#Subsidiestype').combobox('getValue');
					var startM = $('#startM').datebox("getValue");
					var endM = $('#endM').datebox("getValue");
					var derpart = $('#hrDepart').val();
					var hrName = $('#hrName').val();
					if((!derpart || derpart=="") && (!hrName || hrName=="") && type != 0 ){
					  alert("请选择部门或者姓名");
					}
					else {
					   if(type != 0){
						   if((startM || startM!="") && (endM || endM!="")){
							   queryByType();
						   }
						   else alert("请输入开始与结束时间");
					   }else{	
						var param = $("#searchForm").serializeObject();
						$('#data-list').datagrid('reload',param);
							//回调函数
							if(jQuery.isFunction(callback)){
								callback();
							}
					}
				  }
				}
  			},
  			dataGrid:{
  				title:'人头收入归集',
	   			url:urls['msUrl']+'/tostHrSalaryinfoT/dataListAll.do',
				pagination:true,
	   			columns:[[
	   			    {field:'hrName',title:'姓名',width:50,sortable:true},	
					{field:'hsaMonth',title:'工资结算月份',width:80,sortable:true},				
					{field:'hsaPushmoney',title:'提留部分',width:80,sortable:true},				
					{field:'hsaPerformance',title:'绩效/提成',width:70,sortable:true},			
					//{field:'hsuAccount',title:'补贴合计',width:80,sortable:true},			
					//{field:'hfaCount',title:'应发合计',width:80,sortable:true},				
					//{field:'hdeDeductcount',title:'扣款金额',width:80,sortable:true},				
					{field:'hsaPaid',title:'实发金额',width:80,sortable:true},				
					//{field:'hsaDesc',title:'备注',width:80,sortable:true},				
					//{field:'hsaStatus',title:'工资状态',width:80,sortable:true},
					{field:'idx',title:'操作',width:120,align:'center',formatter:function(value,row,index){
						var html ="<a href='"+urls['msUrl']+"/tostHrSalaryinfoT/getById.do?id="+row.id+"'>查看个人详情</a>";
						return html;
					}}
				]],
				toolbar:[
					{id:'',text:'',btnType:''}
				]
			}
		},
		init:function(){
			_box = new YDataGrid(_this.config); 
			_box.init();
			$('#addLine_btn').click(_this.addLine);
			$('#addDefLine_btn').click(_this.addDefBtns);
			$('#delAllLine_btn').click(function(){
				jeecg.confirm('Confirm','Are you sure you want to delete record?',function(r){
					_this.delAllLine(false);
				});
			});
			
		}
	}
	return _this;
}();


//查询部门
$("#hr").combobox({
	url:urls['msUrl']+"/tostHrBaseinfo/queryDepartId.do",
	dataType:'json',
	valueField:'id',
	textField:'hdpName',
	multiple:false,
	formatter:function(result){
		var s= "<span input type='hidden' class='selectId'  id='selectId_"+result.id+"'>"+result.hdpName+"</span>";
		return s;
	},
	onSelect:function(result){
		$("#selectId_"+result.id).attr("checked", true);
		document.getElementById("hrDepart").value=result.id;
	},
	onChange:function(){
		document.getElementById("hrDepart").value="";
    },
	onUnselect:function(result){
		$("#selectId_"+result.id).attr("checked", false);
	}
});

function queryByType(){
	$.ajax({
		url:urls['msUrl']+"/tostHrSalaryinfoT/dataListAll.do",
		data:$('#searchForm').serialize(),
		dataType:'json',
		type:'post',
		success: function(result){
			var str = "<br><a href='/myjhr/tostHrPersonyearinfo/list.do'>返回</a>";
			str += "<table id = 'table' style='overflow:auto;font-style: initial;font-variant: initial;font-weight: initial;line-height: normal;" +
					"text-align: start;white-space: normal;-moz-box-sizing: border-box;border-collapse: separate;border-spacing: 1px;" +
					"display: table;margin-bottom: 0;text-indent: 0;background-color:#E7E7E7;margin-top:8px;' ><tr id='tr' ><td style='width:100px;background-color:#FFFFFF;'>姓名</td></tr><table></br></br>";
			document.getElementById("tablediv").innerHTML= str;
			var resultType = result.resultType;
			if(resultType == 1){//补贴
				setType(result,result.subList);
			}else if(resultType == 2){//餐费补贴
				setType(result,result.hmeMealsubList);
			}else if(resultType == 3){//餐费扣除
				setType(result,result.hmeMealeduList);
			}else if(resultType == 4){//社保
				setType(result,result.socialsecurityList);
			}else if(resultType == 5){//考勤
				setType(result,result.checkinlogList);
			}else if(resultType == 6){//工资
				setType(result,result.salaryinfoList);
			}else if(resultType == 7){//公积金
				setType(result,result.accfundinfoList);
			}
		}
	})
}


$(function(){
	jeecg.TostHrPersonyearinfo.init();
});	
		function fspace(str){	
		var rtstr = "";
		if (str != null) {
		for ( var i = 0; str.length > 0 && i < str.length; i++) {
		rtstr += "";
		}
		}
		return rtstr;
}
function setType(result,typeList){
	var monthLength = result.monthlist.length;
	var hrNameLength = result.hrNamelist.length;
	var typeListSize = typeList.length;
	var sub = new Array();
	var showsub = 0;
	var monthTotal =0;
	var k = 0;
	var gettable = document.getElementById("table");
	for(var i=0;i<monthLength;i++){
		 var mtr = document.getElementById("tr");
		 var monthTd = document.createElement("td");
		 setTdstyle(monthTd)
		 var smonth = document.createTextNode(result.monthlist[i]);
		 monthTd.appendChild(smonth);
		 mtr.appendChild(monthTd);
	}
	 var tr = document.getElementById("tr");
	 var totalTd = document.createElement("td");
	 setTdstyle(totalTd);
	 var stotal = document.createTextNode("合计");
	 totalTd.appendChild(stotal);
	 tr.appendChild(totalTd);
	 
	for(var i=1;i<hrNameLength;i++){
		 //创建tr
		 var ctr = document.createElement("tr");
		 //创建姓名td
		 var  nameTd = document.createElement("td");
		 setTdstyle(nameTd);
		 var  sname = document.createTextNode(result.hrNamelist[i]);
		 nameTd.appendChild(sname);
		 ctr.appendChild(nameTd);
		 //创建月份的td，站位用
		 var monthTd = document.createElement("td");
		 setTdstyle(monthTd);
		 var smonth = document.createTextNode("");
		 monthTd.appendChild(smonth);
		 ctr.appendChild(monthTd);
		  for(var s = 1;s<=monthLength-1;s++){
			 for(var j=0;j<typeListSize;j++){
			 var strsub = typeList[j];
			 sub = strsub.split("_");
			 var subtotal = sub[0];
			 var month = sub[1];
			 var hrName = sub[2];
			 	if(month == result.monthlist[s] && hrName == result.hrNamelist[i]){
				 showsub = subtotal;
				 break;
			 }//循环完都没有找到
			 	else {
			 		k+=1;
			 		if(k==typeListSize){
			 			showsub = 0;
			 			k =0;
			 		}
			 	}
		   }
			 var totalTd = document.createElement("td");
			 setTdstyle(totalTd);
			 var stotal = document.createTextNode(showsub);
			 monthTotal += eval(showsub);
			 totalTd.appendChild(stotal);
			 ctr.appendChild(totalTd);
		 }
		//月份合计
		 var ztotalTd = document.createElement("td");
		 setTdstyle(ztotalTd);
		 var sztotal = document.createTextNode(monthTotal);
		 ztotalTd.appendChild(sztotal);
		 ctr.appendChild(ztotalTd);
		 gettable.appendChild(ctr);
		 monthTotal = 0;
	}
	function setTdstyle(td){
		td.setAttribute('border-bottom-colr','#808080');
		td.setAttribute('border-left-color-ltr-source','physical');
		td.setAttribute('border-left-color-rtl-source','physical');
		td.setAttribute('border-left-color-value','#808080');
		td.setAttribute('border-right-color-ltr-source','physical');
		td.setAttribute('border-right-color-rtl-source','physical');
		td.setAttribute('border-right-color-value','#808080');
		td.setAttribute('border-top-color','#808080');
		td.setAttribute('line-height','1.5');
		td.bgColor = '#FFFFFF';
		td.setAttribute('height','25px');
		td.setAttribute('align','left');
		td.setAttribute('outline-color','#FCFDEE');
		td.setAttribute('width','100px');
	}
	
}


		
